<template>
    <el-dialog v-model="dialogFormVisible" :title="title" @close="closeDialog">
        <el-form :model="addShortLinkForm" ref="addShortLinkRef" :rules="addShortLinkrules">
                        <el-form-item label="短码" :label-width="formLabelWidth"
                                      prop="shorts">
                            <el-input
                                    v-model="addShortLinkForm.shorts"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写短码"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="短链接" :label-width="formLabelWidth"
                                      prop="shortLink">
                            <el-input
                                    v-model="addShortLinkForm.shortLink"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写短链接"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="长链接" :label-width="formLabelWidth"
                                      prop="longLink" v-if="addShortLinkForm.id">
                            <el-input
                                    v-model="addShortLinkForm.longLink"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写长链接"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="创建时间" :label-width="formLabelWidth"
                                      prop="createTime" v-if="addShortLinkForm.id">
                            <el-input
                                    v-model="addShortLinkForm.createTime"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写创建时间"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="标志" :label-width="formLabelWidth"
                                      prop="flag" v-if="addShortLinkForm.id">
                            <el-input
                                    v-model="addShortLinkForm.flag"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写标志"
                            ></el-input>
                        </el-form-item>
        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel()">取消</el-button>
        <el-button type="primary" @click="save()">保存</el-button>
      </span>
        </template>
    </el-dialog>
</template>

<script>
    import {ref} from "vue";
    import {httpService} from "@/utils/httpService";
    import {ElMessage, ElForm} from "element-plus";
    import {vxRule} from "@/utils/validator";

    export default {
        name: "AddShortLink",
        components: {ElForm, ElMessage},
        data() {
            return {
                dialogFormVisible: ref(false),
                formLabelWidth: "120px",
                title: "",
                personnelType: "",
                addShortLinkForm: {
                    shorts: "",
                    shortLink: "",
                    longLink: "",
                    createTime: "",
                    flag: "",
                },
                // 校验规则
                addShortLinkrules: {
                    shorts: vxRule(true, null, "blur"),
                    shortLink: vxRule(true, null, "blur"),
                },
            };
        },
        methods: {
            closeDialog() {
                this.clear();
            },
            cancel() {
                this.dialogFormVisible = false;
                this.clear();
            },
            getShortLinkById(id) {
                httpService("/api/shortLink/queryById", {id: id}, "get").then(
                    (data) => {
                        this.addShortLinkForm = data;
                    }
                );
            },
            clear() {
                this.dialogFormVisible = false;
                //清空form
                    this.addShortLinkForm.id = "";
                    this.addShortLinkForm.shorts = "";
                    this.addShortLinkForm.shortLink = "";
                    this.addShortLinkForm.longLink = "";
                    this.addShortLinkForm.createTime = "";
                    this.addShortLinkForm.flag = "";
            },
            save() {
                this.$refs.addShortLinkRef.validate((valid) => {
                    if (valid) {
                        let params = new FormData(); //创建一个form对象,必须是form对象否则后端接受不到数据
                        params.append("shorts", this.addShortLinkForm.shorts);
                        params.append("shortLink", this.addShortLinkForm.shortLink);
                        params.append("longLink", this.addShortLinkForm.longLink);
                        params.append("createTime", this.addShortLinkForm.createTime);
                        params.append("flag", this.addShortLinkForm.flag);
                        if (this.addShortLinkForm.id) {
                            params.append("id", this.addShortLinkForm.id);
                            httpService("/api/shortLink/edit", params, "post").then(
                                (data) => {
                                    if (data.code == 200) {
                                        ElMessage.success("修改成功");
                                        this.clear();
                                        //刷新父组件的数据
                                        this.$parent.getShortLinkList();
                                    } else {
                                        ElMessage.error("修改失败");
                                    }
                                }
                            );
                        } else {
                            httpService("/api/shortLink/add", params, "post").then(
                                (data) => {
                                    if (data.code == 200) {
                                        ElMessage.success("添加成功");
                                        this.clear();
                                        //刷新父组件的数据
                                        this.$parent.getShortLinkList();
                                    } else {
                                        ElMessage.error("添加失败");
                                    }
                                }
                            );
                        }
                    } else {
                        return false;
                    }
                });
            },
        },
    };
</script>
<style>
</style>